/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.wallet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.common.customize.mysql.utils.MysqlUtil;
import com.pig4cloud.pigx.common.customize.security.utils.SecurityUtil;
import com.pig4cloud.pigx.common.security.service.PigxUser;
import com.pig4cloud.pigx.wallet.dto.ContractAccountDetailPageDTO;
import com.pig4cloud.pigx.wallet.entity.ContractAccountDetail;
import com.pig4cloud.pigx.wallet.enums.WalletContractAccountDetailEnum;
import com.pig4cloud.pigx.wallet.mapper.ContractAccountDetailMapper;
import com.pig4cloud.pigx.wallet.mapstruct.ContractAccountDetailStruct;
import com.pig4cloud.pigx.wallet.service.ContractAccountDetailService;
import com.pig4cloud.pigx.wallet.vo.ContractAccountDetailVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.web3j.utils.Strings;

import java.util.List;
import java.util.Objects;

/**
 * 合约账户明细
 *
 * @since 2022/9/12 22:02
 */
@Service
@AllArgsConstructor
public class ContractAccountDetailServiceImpl extends ServiceImpl<ContractAccountDetailMapper, ContractAccountDetail> implements ContractAccountDetailService {
	/**
	 * 分页查询
	 *
	 * @param page      分页参数
	 * @param condition 查询条件
	 * @return {@link Page <  ContractAccountDetailVO >}
	 * @since 2022/9/13 18:56
	 */
	@Override
	public Page<ContractAccountDetailVO> page(Page<ContractAccountDetail> page, ContractAccountDetailPageDTO condition) {
		PigxUser user = SecurityUtil.getLoginUserNotNull();

		QueryWrapper<ContractAccountDetail> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda()
				.select(ContractAccountDetail::getSymbolName,
						ContractAccountDetail::getBusinessType,
						ContractAccountDetail::getOperationType,
						ContractAccountDetail::getAmount,
						ContractAccountDetail::getCreateTime)
				.eq(ContractAccountDetail::getUserId, user.getId())
				.eq(!Objects.isNull(condition.getSymbolId()), ContractAccountDetail::getSymbolId, condition.getSymbolId())
				.eq(!Strings.isEmpty(condition.getBusinessType()), ContractAccountDetail::getBusinessType, condition.getBusinessType())
				.ge(!Objects.isNull(condition.getStartTime()), ContractAccountDetail::getCreateTime, condition.getStartTime())
				.le(!Objects.isNull(condition.getEndTime()), ContractAccountDetail::getCreateTime, condition.getEndTime())
				.orderBy(true, false, ContractAccountDetail::getCreateTime);

		Page<ContractAccountDetail> listPage = baseMapper.selectPage(page, queryWrapper);

		// 转换为VO
		List<ContractAccountDetailVO> voList = ContractAccountDetailStruct.INSTANCE.list2VoList(listPage.getRecords());
		voList.forEach(vo -> {
			// 翻译字典项
			vo.setBusinessType(WalletContractAccountDetailEnum.BusinessType.findName(vo.getBusinessType()));

			// 操作类型 = 减少，设置金额格式
			if (Objects.equals(WalletContractAccountDetailEnum.OperationType.MINUS.getValue(), vo.getOperationType())) {
				vo.setAmount(vo.getAmount().negate());
			}

			// 设置默认值
			vo.setCurName("USDT");
			vo.setSymbolName(vo.getSymbolName());
		});

		return MysqlUtil.transferPage(listPage, voList);
	}
}
